<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Record extends Model
{
    public static function new($uid, $msg_type, $type, $msg=[]) {
        Redis::LPUSH('LyfenUserRecord:' . $msg_type . ':' . $uid, json_encode([
            'type' => $type,
            'time' => time(),
            'desc' => $msg,
        ]));
    }

    /**
     * 好友投喂记录
     *
     * @param [type] $uid 被投喂玩家ID
     * @param [type] $fuid 投喂玩家ID
     * @return void
     */
    public static function byFeedRecord($uid, $fuid, $name, $avatar) {
        Redis::ZADD('LyfenUserByFeedingRecord:' . $uid, $fuid, json_encode([
            'fuid' => $fuid,
            'name' => $name,
            'avatar' => $avatar,
            'time' => time()
        ]));
    }

    /**
     * 被投喂列表, 获取一次即删除
     *
     * @param [type] $uid
     * @return array
     */
    public static function byFeedRecords($uid) {
        $key = 'LyfenUserByFeedingRecord:' . $uid;
        $records = Redis::ZRANGE($key, 0, -1);
        Redis::DEL($key);
        return $records ?? [];
    }
}
